﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using System.Collections;
using Microsoft.SharePoint.Utilities;
using System.Web;

namespace PropertyManagementFeatures.FormCode
{
    public class PropertyEvaluationTaskForm : LayoutsPageBase
    {

        protected string ListId;
        protected SPList TaskList;
        protected SPListItem TaskItem;
        protected Guid WorkflowInstanceId;
        protected SPWorkflow WorkflowInstance;
        protected SPList ItemList;
        protected SPListItem Item;
        protected SPWorkflowTask Task;

        protected string TaskData;
        protected string TaskName;
        protected SPWorkflowAssociation WorkflowAssociation;

        protected RadioButton radOfferOver;
        protected RadioButton radFixedPrice;
        protected InputFormTextBox uxPrice;

        protected string PageTitle;
        protected string PageTitleInArea;
        protected string PageDescription;

        protected override void OnLoad(EventArgs ea)
        {

            ListId = Request.QueryString["List"];
            TaskList = Web.Lists[new Guid(ListId)];
            TaskItem = TaskList.GetItemById(Convert.ToInt32(Request.Params["ID"]));
            WorkflowInstanceId = new Guid((string)TaskItem["WorkflowInstanceID"]);
            WorkflowInstance = new SPWorkflow(Web, WorkflowInstanceId);
            Task = WorkflowInstance.Tasks[0];
            ItemList = WorkflowInstance.ParentList;
            Item = ItemList.GetItemById(WorkflowInstance.ItemId);

            WorkflowAssociation = ItemList.WorkflowAssociations[WorkflowInstance.AssociationId];

            TaskName = TaskItem["Title"].ToString();

            string ItemName = Item.Title;

            PageTitle = "Property Sale Task";
            PageTitleInArea = "Property Sale";
            PageDescription = "Please evaluate the property and input the evaluated price";

        }

        public void submit_OnClick(object sender, EventArgs e)
        {
            string priceType = "";
            if (this.radFixedPrice.Checked) priceType = "Fixed Price";
            if (this.radOfferOver.Checked) priceType = "Offer Over";
 

            Hashtable taskHash = new Hashtable();
            taskHash["Price Type"] = priceType;
            taskHash["Property Price"] = uxPrice.Text;
            taskHash["TaskStatus"] = "Completed";
            SPWorkflowTask.AlterTask(TaskItem, taskHash, true);

            Item["Price Type"] = priceType;
            Item["Property Price"] = Double.Parse(uxPrice.Text);
            Item["Property_x0020_Status"] = "Evaluated";
            Item.Update();

            SPUtility.Redirect(ItemList.DefaultViewUrl, SPRedirectFlags.UseSource, HttpContext.Current);
        }

        public void cancel_OnClick(object sender, EventArgs e)
        {
            SPUtility.Redirect(ItemList.DefaultViewUrl, SPRedirectFlags.UseSource, HttpContext.Current);
        }

    }
}

